package com.zqwm.fly.http;

import com.zqwm.fly.model.Article;
import com.zqwm.fly.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.List;

@Controller
@RequestMapping(value = "/article")
public class ArticleAction
{
	@Autowired
	private ArticleService articleService;

	//获取文章详情页
	@RequestMapping(value = "/detail/{id}/{active}",method = RequestMethod.GET)
	public String articleDetail(ModelMap map,@PathVariable Integer id,@PathVariable String active)
	{
		Article article = new Article();
		article.setId(id);
		boolean flag =true;
		List<Article>articleList = articleService.list(article);
		if(null != articleList && articleList.size()>0)
		{
			map.put("article",articleList.get(0));
			articleService.setLookSum(articleList.get(0));
		}else{
			int nid =id;
			while (flag)
			{
				if(nid - id>100 || id - nid>100){if("+".equals(active)){article.setId(nid - 1); }else{article.setId(nid + 1);}}else {
					if("+".equals(active)){id += 1; article.setId(id); }else{ id -= 1;article.setId(id);}
				}

				articleList = articleService.list(article);
				if(null != articleList && articleList.size()>0) { map.put("article",articleList.get(0));articleService.setLookSum(articleList.get(0));flag=false; }
			};
		}

		return "/ftl/article";
	}




	/**
	 * 4、通过@PathVariable获取路径中的参数
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
	public String addUser4(ModelMap map,@PathVariable String username, @PathVariable String password) {
		System.out.println("username is:"+username);
		System.out.println("password is:"+password);
		return "demo/index";
	}

	//评论文章
	public String discussarticle(){
		

		
		return "json";
	}

	/**
	   * 根据IP地址获取mac地址
	   * @param ipAddress 127.0.0.1
	   * @return
	   * @throws SocketException
	   * @throws UnknownHostException
	   */
	  public static String getLocalMac(String ipAddress) throws SocketException,UnknownHostException
	  {
	    String str = "",macAddress = "";
	    final String LOOPBACK_ADDRESS = "127.0.0.1";
	    // 如果为127.0.0.1,则获取本地MAC地址。
	    if (LOOPBACK_ADDRESS.equals(ipAddress)) {
	      InetAddress inetAddress = InetAddress.getLocalHost();
	      byte[] mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();// 貌似此方法需要JDK1.6。
	      // 下面代码是把mac地址拼装成String
	      StringBuilder sb = new StringBuilder();
	      for (int i = 0; i < mac.length; i++) {
	        if (i != 0) {
	          sb.append("-");
	        }
	        // mac[i] & 0xFF 是为了把byte转化为正整数
	        String s = Integer.toHexString(mac[i] & 0xFF);
	        sb.append(s.length() == 1 ? 0 + s : s);
	      }
	      // 把字符串所有小写字母改为大写成为正规的mac地址并返回
	      macAddress = sb.toString().trim().toUpperCase();
	      return macAddress;
	    } else {
	      // 获取非本地IP的MAC地址
	      try {
	        System.out.println(ipAddress);
	        Process p = Runtime.getRuntime()
	            .exec("nbtstat -A " + ipAddress);
	        System.out.println("===process=="+p);
	        InputStreamReader ir = new InputStreamReader(p.getInputStream());
	          
	        BufferedReader br = new BufferedReader(ir);
	        
	        while ((str = br.readLine()) != null) {
	          if(str.indexOf("MAC")>1){
	            macAddress = str.substring(str.indexOf("MAC")+9, str.length());
	            macAddress = macAddress.trim();
	            System.out.println("macAddress:" + macAddress);
	            break;
	          }
	        }
	        p.destroy();
	        br.close();
	        ir.close();
	      } catch (IOException ex) {
	      }
	      return macAddress;
	    }
	  }
	  
	//获取文章列表
	public String listArticle() throws IOException
	{

		return "json";
	}
	
	public String updatelooksum()
	{

		
		return "json";
	}

}
